#include <iostream>
#include <vector>
#define MAXN 100
using namespace std;
typedef pair<int, int> Product;
Product products[MAXN];
int dp[MAXN + 5][MAXN + 5];
// dp[i][j] 从第i个开始挑, 质量不超过j的最大值
// dp[i][j] = dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]
int n, w;
void solve();
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) 
        cin >> products[i].first >> products[i].second;
    cin >> w;
    solve();
}


// 背包问题0-1背包 逆序
void solve() {
    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j <= w; j++) {
            dp[i][j] = dp[i + 1][j];
            if (j >= products[i].first) 
                dp[i][j] = max(dp[i][j], dp[i + 1][j - products[i].first] + products[i].second);
        }
    }
}

void solve2() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= w; j++) {
            dp[i + 1][j] = dp[i][j];
            if (j >= products[i].first)
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - products[i].first] + products[i].second);
        }
    }
    
}